Integrate R with PHP

I would like build a web application using R. I am using Windows Vista and have an Apache server. I have tried Rpad, but I was not able to correctly configure it. How do I set up Rpad as I am not that well off with PHP and Apache server? Or are there other ways to use R on Apache server?


Answers:

Here is the easiest way to do it that I found:

This implementation of PHP and R consists of only two files. One written in PHP, and the other an R script. The PHP returns a form which uses the GET method to send a variable N to the server. When the form is submitted, the PHP will then execute an R script from the shell using a combination of the PHP command exec() and the Rscript shell command. This command will pass the variable N to the R script. The R script will then execute and save a histogram plot of N normally distributed values to the filesystem. Finally, when the R script is complete, the PHP will return the HTML tag containing the saved images path. First, the PHP file

< ?php
// poorman.php     
echo "< form action='poorman.php' method='get'>";    
echo "Number values to generate: < input type='text' name='N' />";    
echo "< input type='submit' />";    
echo "< /form>";     
if( isset($_GET['N']))    
{
  $N = $_GET['N'];

  // execute R script from shell    
  // this will save a plot at temp.png to the filesystem

  exec("Rscript my_rscript.R $N");

  // return image tag

  $nocache = rand();

  echo("< img src='temp.png?$nocache' /> ");    
}    
?>

and the R script

# my_rscript.R     
args <- commandArgs(TRUE)     
N <- args[1]    
x <- rnorm(N,0,1)     
png(filename="temp.png", width=500, height=500)
hist(x, col="lightblue")    
dev.off()

Here are some more you are welcome to try:

  1. http://danpolant.com/r-integration-with-php/

  2. http://steve-chen.net/document/r/r_php